package com.example.mainstream.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import androidx.lifecycle.ViewModelProvider;

import com.example.shopapp.App;
import com.example.shopapp.MainActivity;
import com.example.shopapp.R;
import com.example.shopapp.bean.Merchant;
import com.example.shopapp.bean.User;
import com.example.shopapp.databinding.ActivityLoginBinding;
import com.example.shopapp.model.IMerchantListener;
import com.example.shopapp.model.IUserLoginListener;
import com.example.shopapp.model.LoginViewModel;
import com.example.shopapp.ui.merchant.MerchantMainActivity;
import com.example.shopapp.ui.user.UserMainActivity;

import cn.project.base.baseui.BaseNormalActivity;

/**
 * 登录页面
 */
public class LoginActivity extends BaseNormalActivity<ActivityLoginBinding> {
    private int standing = 0;//身份 0 用户 1商家
    LoginViewModel loginViewModel;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
    }

    @Override
    protected void initData() {

        // 设置下拉框的选择监听器
        mBinding.spShenfen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                standing = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        // 设置注册按钮的点击监听器
        mBinding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });

        // 设置登录按钮的点击监听器
        mBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mBinding.inputName.getText().toString();
                String password = mBinding.inputPassword.getText().toString();

                // 判断用户名是否为空
                if (TextUtils.isEmpty(name)){
                    showToast("请输入用户名");
                    return;
                }

                // 判断密码是否为空
                if (TextUtils.isEmpty(password)){
                    showToast("请输入密码");
                    return;
                }

                // 根据用户选择的身份进行登录
                if (standing == 0){
                    userLogin(name,password);
                    return;
                }
                merchantLogin(name,password);
            }
        });
    }

    /**
     * 用户登录
     * @param name 用户名
     * @param password 密码
     */
    private void userLogin(String name,String password){
        // 调用loginViewModel的userLogin方法，传入用户名、密码和用户登录监听器
        loginViewModel.userLogin(name, password, new IUserLoginListener() {
            @Override
            public void userLoginSuccess(User user) {

                App.getInstance().user = user;
                startActivity(new Intent(LoginActivity.this, UserMainActivity.class));
                finish();
            }

            @Override
            public void userLoginFail(String message) {
                // 调用showToast方法显示登录失败的消息
                showToast(message);
            }
        });
    }

    /**
     * 商家登录
     * @param name 用户名
     * @param password 密码
     */
    private void merchantLogin(String name,String password){
        // 调用loginViewModel的merchantLogin方法，传入用户名、密码和商家登录监听器
        loginViewModel.merchantLogin(name, password, new IMerchantListener() {
            @Override
            public void merchantLoginSuccess(Merchant merchant) {
                App.getInstance().merchant = merchant;
                startActivity(new Intent(LoginActivity.this, MerchantMainActivity.class));
                finish();
            }

            @Override
            public void merchantLoginFail(String message) {
                // 调用showToast方法显示登录失败的消息
                showToast(message);
            }
        });
    }
}